
    /**  
    * @Title: StatemachFieldAuthServiceImpl.java
    * @Package com.efast.cafe.portal.service.statemach.impl
    * @Description: TODO(用一句话描述该文件做什么)
    * @author Administrator
    * @date 2019年3月20日
    * @version V1.0  
    */
    
package com.efast.cafe.portal.service.statemach.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.efast.cafe.commponent.statemach.bean.PortalStatemachFieldAuth;
import com.efast.cafe.commponent.statemach.bean.StatemachFieldAuthQueryBean;
import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.cafe.portal.bean.statemach.PortalStateMachStateConf;
import com.efast.cafe.portal.bean.statemach.PortalStatemachFieldAuthSaveBean;
import com.efast.cafe.portal.bean.statemach.PortalStatemachFieldAuthSaveDetailBean;
import com.efast.cafe.portal.dao.statemach.PortalStateMachStateDao;
import com.efast.cafe.portal.dao.statemach.PortalStatemachFieldAuthDao;
import com.efast.cafe.portal.service.statemach.IPortalStatemachFieldAuthService;
import com.efast.cafe.util.StringUtil;

import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

@Service
@RestController
public class PortalStatemachFieldAuthServiceImpl implements IPortalStatemachFieldAuthService{

	@Autowired
	private PortalStatemachFieldAuthDao portalStatemachFieldAuthDao;

	
	   
	@Override
	public List<PortalStatemachFieldAuth> queryStatemachAuth(StatemachFieldAuthQueryBean queryBean) {
		Example example = new Example(PortalStatemachFieldAuth.class);
		if(null!=queryBean){
			Criteria criteria = example.createCriteria();
			if(queryBean.getConfId()!=null){
				criteria.andEqualTo("confId",queryBean.getConfId());
			}
			if(queryBean.getStateId()!=null){
				criteria.andEqualTo("stateId", queryBean.getStateId());
			}
			if(queryBean.getEntityIds()!=null && queryBean.getEntityIds().length>0){
				criteria.andIn("entityId", Arrays.asList(queryBean.getEntityIds()));
			}
			if(StringUtil.isNotBlank(queryBean.getType())){
				criteria.andEqualTo("type", queryBean.getType());
			}
			if(queryBean.getAuthId()!=null){
				criteria.andEqualTo("authId", queryBean.getAuthId());
			}
		}
		return portalStatemachFieldAuthDao.selectByExample(example);
		
	}

	@Autowired
	private PortalStateMachStateDao portalStateMachStateDao;


	    
	@Override
	public void saveStatemachFieldAuth(PortalStatemachFieldAuthSaveBean auth) {
			Example example = new Example(PortalStatemachFieldAuth.class);
			Criteria criteria = example.createCriteria();
			criteria.andEqualTo("confId",auth.getConfId())
			.andEqualTo("stateId", auth.getStateId());
			portalStatemachFieldAuthDao.deleteByExample(example);
		if(auth.getAuths()!=null && auth.getAuths().length>0){
			List<PortalStatemachFieldAuth> insertList = new ArrayList<>();
			for(PortalStatemachFieldAuthSaveDetailBean a : auth.getAuths()){
				
				if(StringUtil.isNotBlank(a.getInvisibles()) || StringUtil.isNotBlank(a.getNonEditables())
						|| StringUtil.isNotBlank(a.getActions()) || StringUtil.isNotBlank(a.getRequired()) ||
						StringUtil.isNotBlank(a.getToolbars())){
					PortalStatemachFieldAuth au  = new PortalStatemachFieldAuth();
					au.setAuthId(a.getAuthId());
					au.setConfId(auth.getConfId());
					au.setEntityId(a.getEntityId());
					au.setInvisibles(a.getInvisibles());
					au.setNonEditables(a.getNonEditables());
					au.setStateId(auth.getStateId());
					au.setType(a.getType());
					au.setActions(a.getActions());
					au.setToolbars(a.getToolbars());
					au.setRequired(a.getRequired());
					insertList.add(au);
				}
			}
			if(insertList.size()>0){
				portalStatemachFieldAuthDao.insertList(insertList);
			}
		}
		
		portalStateMachStateDao.deleteStateConf(SpringWebUtils.getRequestCompany(), auth.getConfId(), auth.getStateId());
		
		PortalStateMachStateConf conf = new PortalStateMachStateConf();
		conf.setCompanyCode(SpringWebUtils.getRequestCompany());
		conf.setConfId(auth.getConfId().longValue());
		conf.setStateId(auth.getStateId().longValue());
		conf.setExtForm(auth.getExtForm());
		conf.setViewCode(auth.getViewCode());
		portalStateMachStateDao.insertStateConf(conf);
		
		
		
	}




	
	    /* (非 Javadoc)
	    * 
	    * 
	    * @param confid
	    * @param stateId
	    * @param entityName
	    * @return
	    * @see com.efast.cafe.portal.service.statemach.IPortalStatemachFieldAuthService#queryFieldAuthBy(java.lang.Integer, java.lang.Integer, java.lang.String)
	    */
	    
	@Override
	public Map<String, List<String>> queryFieldAuthBy(Integer confid, Integer stateId, String entityName) {
		List<PortalStatemachFieldAuth> list = 
				portalStatemachFieldAuthDao.queryFieldAuthBy(confid, stateId, entityName, SpringWebUtils.getRequestCompany(), SpringWebUtils.getRequestUser());
		List<String> invisibles = new ArrayList<>();//不可见字段
		List<String> non_editables = new ArrayList<>();//不可用字段
		List<String> required = new ArrayList<>();
		List<String> actions = new ArrayList<>();
		List<String> toolbars = new ArrayList<>();
		for(PortalStatemachFieldAuth auth : list){
			if(StringUtil.isNotBlank(auth.getInvisibles())){
				invisibles = Arrays.asList(auth.getInvisibles().split(","));
			}
			if(StringUtil.isNotBlank(auth.getNonEditables())){
				non_editables = Arrays.asList(auth.getNonEditables().split(","));
			}
			if(StringUtil.isNotBlank(auth.getRequired())){
				required =  Arrays.asList(auth.getRequired().split(","));
			}
			if(StringUtil.isNotBlank(auth.getActions())){
				actions =  Arrays.asList(auth.getActions().split(","));
			}
			if(StringUtil.isNotBlank(auth.getToolbars())){
				toolbars =  Arrays.asList(auth.getToolbars().split(","));
			}
		}
		Map<String, List<String>> map = new HashMap<>();
		map.put("invisibles", invisibles);
		map.put("non_editables", non_editables);
		map.put("required", required);
		map.put("actions", actions);
		map.put("toolbars", toolbars);
		return map;
	}
	
}
